import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;

public class hash_test {
    public static byte[] SHAIChecksum(InputStream is) throws Exception {
        byte[] buffer = new byte[1024];
        MessageDigest complete = MessageDigest.getInstance("SHA-1");
        int numRead = 0;
        do{
            numRead = is.read(buffer);
            if (numRead > 0){
                complete.update(buffer, 0 , numRead);
            }
        } while (numRead != -1);
        is.close();
        return complete.digest();
    }



    public static void dfs(String path) {
        File dir = new File(path);
        File[] fs = dir.listFiles();
        for(int i = 0; i < fs.length; i++){
            if(fs[i].isFile()){
                String subPath = path + fs[i].getName();
                try{
                    File file = new File(subPath);
                    FileInputStream is = new FileInputStream(file);
                    byte[] sha1 = SHAIChecksum(is);

                    String result = "";
                    for (int j = 0; j < sha1.length; j++){
                        result += Integer.toString(sha1[j]&0xFF, 16);
                    }

                    System.out.println(result);

                } catch (Exception e){
                    e.printStackTrace();
                }
            }
            if(fs[i].isDirectory()){
                System.out.println("directory" + fs[i].getName());
                dfs(path + File.separator + fs[i].getName());
            }
        }

    }

    public static void main(String[] args) {
        try {
            dfs("E:/test/");
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
